﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using seo.Data;

namespace seo.DAO
{
    public class KeywordDao
    {
        private MySqlConnection conn;

        public KeywordDao(MySqlConnection conn)
        {
            this.conn = conn;
        }

        public List<Keyword> getAllKeyword()
        {

            List<Keyword> list = new List<Keyword>();

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,siteid,keyword,keyword_weight,keywordchinese from keyword where deleteflg='0'";

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                Keyword keyword = new Keyword();

                keyword.id = reader.GetInt32(0);
                keyword.siteid = reader.GetInt32(1);
                keyword.keyword = reader.GetString(2);
                keyword.keyword_weight = reader.GetInt32(3);
                keyword.keywordchinese = reader.GetString(4);

                list.Add(keyword);
            }

            reader.Close();

            return list;
        }

        public int getTotalKeywordWeight(int siteid)
        {
            int totalkeywordweight = 0;

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select sum(keyword_weight) as totalweight from keyword where deleteflg='0' and siteid = " + siteid;

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                totalkeywordweight = reader.GetInt32(0);
            }

            reader.Close();

            return totalkeywordweight;
        }

        public List<Keyword> getKeywordListBySiteId(int siteid)
        {

            List<Keyword> list = new List<Keyword>();

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,siteid,keyword,keyword_weight,keywordchinese from keyword where deleteflg='0' and siteid = " + siteid;

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                Keyword keyword = new Keyword();

                keyword.id = reader.GetInt32(0);
                keyword.siteid = reader.GetInt32(1);
                keyword.keyword = reader.GetString(2);
                keyword.keyword_weight = reader.GetInt32(3);
                keyword.keywordchinese = reader.GetString(4);

                list.Add(keyword);
            }

            return list;
        }
    }
}
